package com.projectexercise.test.verify;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RequestMapping("/admin")
@RestController
public class VerifyController {
    private static final String SESSION_KEY = "KAPTCHA_SESSION_KEY";
    private static final String SESSION_DATE = "KAPTCHA_SESSION_DATE";
    private final static long VALID_MILLIS_TIME = 60 * 1000;
    @RequestMapping("/check")
    public Boolean check(String captcha, HttpSession session){
        if (!StringUtils.hasLength(captcha)){
            return false;
        }
        String string = (String) session.getAttribute(SESSION_KEY);
        Date date = (Date) session.getAttribute(SESSION_DATE);
        if(captcha.equalsIgnoreCase(string)){
            if(System.currentTimeMillis() - date.getTime() < VALID_MILLIS_TIME){
                return true;
            }
        }
        return false;
    }
}
